package com.example.online_exam_system.controller;

import com.example.online_exam_system.commons.Result;
import com.example.online_exam_system.commons.TableResult;
import com.example.online_exam_system.dto.QuestionsDto;
import com.example.online_exam_system.entity.StuSub;
import com.example.online_exam_system.logic.ITeacherLogicService;
import com.example.online_exam_system.service.StuSubService;
import com.example.online_exam_system.utils.HAUtils;
import com.example.online_exam_system.utils.MD5Utils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description 教师业务控制层
 * @Author lianglong
 * @Since 2021/12/1 0:50
 **/
@Slf4j
@RestController
public class TeacherController {

	@Autowired
	HttpSession session;

	@Autowired
	ITeacherLogicService teacherLogicService;




	/**
	 * @description 新增试题
	 * @param questionsDto 添加试题的Dto
	 * @return RESULT
	 * @author lianglong
	 */
	@RequestMapping("/insertQuestion")
	public Result insertQuestion(@RequestBody QuestionsDto questionsDto){
		return teacherLogicService.insertQuestion(questionsDto);
	}

	/**
	 * @description 获取试题列表
	 * @param
	 * @return tablereulst
	 * @author lianglong
	 */
	@RequestMapping("/getquestionsTable")
	public TableResult getquestionsTable(){
		//从session中去取用户姓名
		String username =(String) session.getAttribute("username");
		return teacherLogicService.getQuestionsTable(username);
	}

	/**
	 * @description 更新试题
	 * @param
	 * @return
	 * @author lianglong
	 */
	@RequestMapping("updateQuestion")
	public Result updateQuestion(@RequestBody Map<String,Object> inParam){
		int i = teacherLogicService.updateQuestion(inParam);
		if(i>=1){
			return new Result().success(true).msg("修改成功").data("哈哈哈");
		}else{
			return new Result().success(false).msg("修改失败").data("");
		}
	}


	/**
	 * @description 删除试题
	 * @param inParam  试题id
	 * @return Result
	 * @author lianglong
	 */
	@RequestMapping("deleteQuestion")
	public Result deleteQuestion(@RequestBody Map<String,Object> inParam){
		int i = teacherLogicService.deleteQuestion(inParam);
		if(i>=1){
			return new Result().success(true).msg("删除成功").data("哈哈哈");
		}else{
			return new Result().success(false).msg("删除失败").data("");
		}
	}

	/**
	 * @description 检验教师密码是否正确
	 * @param
	 * @return
	 * @author lianglong
	 */
	@RequestMapping(value = "/pwdVerify",method = RequestMethod.POST)
	public Result pwdVerify(@RequestBody Map<String,Object> inParam){
		int i = teacherLogicService.pwdVerify(inParam);
		if(i>0){
			return new Result().success(true).msg("校验成功");
		}else{
			return new Result().success(false).msg("校验失败");
		}

	}

	/**
	 * @description 更新教师信息
	 * @param
	 * @return
	 * @author lianglong
	 */
	@RequestMapping(value = "updateTeacherInfo",method = RequestMethod.POST)
	public Result updateTeacherInfo(@RequestBody Map<String,Object> inParam){
		//更新基本信息
		int i = teacherLogicService.updateTeacherInfo(inParam);
		if(i == 1){
			return new Result().success(true).msg("更新成功").data("");
		}else{
			return new Result().success(false).msg("更新失败").data("");
		}
	}


	/**
	 * @description 添加试卷时显示的试题信息,供老师选择试题
	 * @param
	 * @return
	 * @author lianglong
	 */
	@PostMapping("/queryexamquestions")
	public TableResult getQuestions(@RequestBody Map<String,Object> inParam){
		return teacherLogicService.getQuestions(inParam);
	}

	/**
	 * @description 新增试卷所选试题列表
	 * @param
	 * @return
	 * @author lianglong
	 */
	@PostMapping("/getquestionbyid")
	public TableResult getquestionbyid(@RequestBody Map<String,Object> inParam){
		return  teacherLogicService.getquestionbyid(inParam);
	}

	@PostMapping("/addExamPage")
	public Result addExamPage(@RequestBody Map<String,Object> inParam){
		return teacherLogicService.addExamPage(inParam);
	}

	@PostMapping("/addExamPage2")
	public Result addExamPage2(@RequestBody Map<String, Object> inParam) {

		return teacherLogicService.addExamPage2(inParam);
	}

	@PostMapping("/queryExam")
	public TableResult queryExam(){
		return teacherLogicService.queryExam();
	}

	/**
	 * 教师页面成绩查询
	 * @return
	 */
	@PostMapping("queryScore1")
	public TableResult queryScore1(){
		String username = session.getAttribute("username").toString();
		log.debug("username:"+username);
		TableResult tr = teacherLogicService.queryScore1(username);
		//return teacherLogicService.queryScore1(username);
		return tr;
	}

}
